﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UBlockly.Sys
{
    /// <summary>
    /// Block资源器
    /// </summary>
    public class BlockLoaderSystem 
    {
        IBlockLoader blockLoader;

        public void SetLoaderAgent(IBlockLoader loader)
        {
            blockLoader = loader;
        }

        public bool IsLoaderInit()
        {
            return blockLoader != null;
        }

        public string GetConfigPath()
        {
            return blockLoader.CustomConfigPath;
        }

        public void LoadResource<T>(string assetName, System.Action<T> callback) where T : Object
        {
            if (blockLoader == null)
                DefaultLoad<T>(assetName, callback);
            else
                blockLoader.LoadResource<T>(assetName, callback);
        }

        /// <summary>
        /// 默认加载器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assetName"></param>
        /// <param name="callback"></param>
        void DefaultLoad<T>(string assetName, System.Action<T> callback) where T : Object
        {
#if UNITY_EDITOR
            T obj = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(GetDefaultResPath(assetName));
            Debug.Log($"加载{assetName}成功");
            callback(obj);
#else
                       throw new System.NullReferenceException("为实现DefaultLoad");
#endif
        }
        string GetDefaultResPath(string assetName)
        {
            return $"Assets/BlockRes/{assetName}";
        }
    }
}
